package com.crm.common.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;

import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.apache.http.cookie.Cookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.*;
import java.net.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

/**
 * @author yuanyunfeng
 * @date 2021/4/21 13:51
 */
public class WebUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebUtil.class);
    /**
     * 返回成功状态码
     */
    private static final int SUCCESS_CODE = 200;


    /**
     * 对指定网址发送get请求，传入cookie
     *
     * @param url
     * @param cookie
     * @return
     */
    public static String getPageContent(String url, String cookie, String host) {

        StringBuffer sb = new StringBuffer();
        try {
            // 建立连接
            URL u = new URL(url);
            HttpURLConnection httpUrlConn = (HttpURLConnection) u.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestMethod("GET");

            //设置cookie
            if (StringUtils.isNotBlank(cookie)) {
                httpUrlConn.setRequestProperty("Cookie", cookie);
            }
            httpUrlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36");

            if (StringUtils.isNotBlank(host)) {
                httpUrlConn.setRequestProperty("Host", host);
            }
//            httpUrlConn.setRequestProperty("Charset", "utf-8");

//            httpUrlConn.setRequestProperty("Referer","https://shuidi.cn/");
            // 获取输入流
            InputStream is = httpUrlConn.getInputStream();
            // 将字节输入流转换为字符输入流
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            // 为字符输入流添加缓冲
            BufferedReader br = new BufferedReader(isr);

            if (httpUrlConn.getResponseCode() == SUCCESS_CODE) {
                // 读取返回结果
                String data = null;
                while ((data = br.readLine()) != null) {

                    sb.append(data);
                    System.out.println(data);
                }
            }
            // 释放资源
            br.close();
            isr.close();
            is.close();
            httpUrlConn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


    public static String getPageContent1(String url, String cookie, String host) throws Exception {

        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                HttpGet httpGet = new HttpGet(url);
                httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.49");
                httpGet.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
                httpGet.setHeader("Cookie", cookie);
                httpGet.setHeader("Host", host);
                client = HttpClients.createDefault();
                response = client.execute(httpGet);

                org.apache.http.HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "utf8");
                System.out.println(result);
                return result;
            } finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 根据url获取cookie
     *
     * @param
     * @return
     * @author yuanyunfeng
     * @date 2021/4/22 14:01
     */
    public static String getCookie(String url) {
        // 全局请求设置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
        // 创建cookie store的本地实例
        CookieStore cookieStore = new BasicCookieStore();
        // 创建HttpClient上下文
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        // 创建一个HttpClient
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse res = null;
        // 创建一个get请求用来获取必要的Cookie，如_xsrf信息
        HttpGet get = new HttpGet(url);
        String cookie = "";
        try {
            res = httpClient.execute(get, context);
            System.out.println("打印这个最长的cookie===" + cookieStore.getCookies());

            // 获取常用Cookie,包括_xsrf信息
            System.out.println("获取的常规Cookie:===============");
            for (Cookie c : cookieStore.getCookies()) {
                cookie = c.getName() + ": " + c.getValue();
                System.out.println(cookie);
            }
            res.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cookie;
    }


    /**
     * 获取跳转链接
     *
     * @param url           请求地址
     * @param linkClassName 跳转链接的class名字   企查查-- maininfo
     * @return cookie 、 url
     * @throws UnsupportedEncodingException
     */
    public static Map<String, String> getJumpUrl(String url, String linkClassName) throws UnsupportedEncodingException {
        System.out.println("打印请求地址是：" + url);
        String cookie = getCookie(url);
        Map<String, String> map = new HashMap<>();
        //拿cookie去获取a标签
        String html = getPageContent(url, cookie, null);
        Document document = Jsoup.parse(html);
        //获得文档下所有class属性为header的标签，返回的是一个标签的集合
        Elements elements = document.getElementsByClass(linkClassName);
        System.out.println("打印elements的长度：" + elements.size());
        String attr = elements.get(0).select("a").attr("href");
//        String newLink = attr = "https://shuidi.cn" + attr;
        System.out.println("dayin这个地址是：" + attr);
        map.put("cookie", cookie);
        map.put("url", attr);
        return map;
    }


    public static Map<String, String> jsoupDemo(String url) {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        // https://www.qcc.com/firm/4b9156b876105261e7c08a7562fad1fb.html
        // HttpGet httpGet = new HttpGet("https://www.qcc.com/firm/4b9156b876105261e7c08a7562fad1fb.html");
        HttpGet httpGet = new HttpGet(url);
        RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(2000).build();
        httpGet.setConfig(config);
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.49");
        httpGet.setHeader("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Microsoft Edge\";v=\"90\"");
        httpGet.setHeader("sec-ch-ua-mobile", "?0");
        httpGet.setHeader("sec-fetch-dest", "document");
        httpGet.setHeader("pragma", "no-cache");
        httpGet.setHeader("cache-control", "no-cache");
        httpGet.setHeader("pragma", "no-cache");
        httpGet.setHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5,ja;q=0.4");
        httpGet.setHeader("accept-encoding", "gzip, deflate, br");
        httpGet.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");


        CloseableHttpResponse httpResponse = null;
        Map<String, String> resMap = new HashMap<>();

        try {
            httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                org.apache.http.HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity, "utf8");
                System.out.println(result);
//                log.info(result);
                Document document = Jsoup.parse(result);
                String title = document.getElementsByTag("title").first().text();
                System.out.println(title);
                Element tagOne = document.getElementById("Cominfo");
                Elements trs = tagOne.getElementsByTag("tr");

                for (Element tr : trs) {
                    Elements tds = tr.getElementsByTag("td");
                    for (int i = 0; i < tds.size(); i++) {
                        if (i % 2 == 0) {
                            String name = tr.getElementsByTag("td").get(i).text();
                            String value = tr.getElementsByTag("td").get(i + 1).text();
                            resMap.put(name, value);
                        }
                    }
                }
                resMap.forEach((k, v) -> {
                    System.out.println("key:{}" + k + "value:{}" + v);
                });

                EntityUtils.consume(entity);
            }
        } catch (IOException e) {
            System.out.println("处理异常【{}】" + e);
//            log.error("处理异常【{}】",e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return resMap;

    }


    public static Map<String, String> getJumpUrl1(String url, String linkClassName, String host) throws Exception {
        System.out.println("打印请求地址是：" + url);
        String cookie = getCookie(url);
        Map<String, String> map = new HashMap<>();
        //拿cookie去获取a标签
        String html = getPageContent1(url, cookie, host);
        Document document = Jsoup.parse(html, "utf-8");
        //获得文档下所有class属性为header的标签，返回的是一个标签的集合
        Elements elements = document.getElementsByClass(linkClassName);
        System.out.println("打印elements的长度：" + elements.size());
        String attr = elements.get(0).select("a").attr("href");
//        String newLink = attr = "https://shuidi.cn" + attr;
        System.out.println("dayin这个地址是：" + attr);
        map.put("cookie", cookie);
        map.put("url", attr);
        return map;
    }


    /**
     * 解析html
     *
     * @param html
     * @param tableClassName
     * @return
     * @author yuanyunfeng
     * @date 2021/4/25 16:18
     */
    public static Map<String, String> resolveHtml(String html, String tableClassName) {
        Document document = Jsoup.parse(html);
        //获得文档中指定class内容
        Elements elements = document.getElementsByClass(tableClassName);
        System.out.println("打印elements的长度：" + elements.size());
        List<String> list = new ArrayList<>();
        Element element = null;
        //从这一个标签集合中一个一个的取出
        for (int i = 0; i < elements.size(); i++) {
            element = elements.get(i);
        }
        Elements trs = elements.select("table").select("tr");
        for (int i = 0; i < trs.size(); i++) {
            Elements tds = trs.get(i).select("td");
            String t1 = tds.text();
            for (int j = 0; j < tds.size(); j++) {
                String tbStr = "";
                String name = tds.get(j).getElementsByClass("seo font-20").text();
                if (StringUtils.isNotBlank(name)) {
                    tbStr = name;
                    System.out.println("查询名字时做的处理:" + name);
                } else {
                    tbStr = tds.get(j).text();
                }
                System.out.println("打印key的名字:" + tbStr);
                list.add(tbStr);
            }
        }
        System.out.println("打印最终的list：" + new Gson().toJson(list));
        Map<String, String> resultMap = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            //放value
            resultMap.put(list.get(i), list.get(i + 1));
            list.remove(i);
        }
        return resultMap;
    }


    /**
     * 解析绿盾html
     *
     * @param html
     * @param tableClassName
     * @return
     */
    public static Map<String, String> resolveLDHtml(String html, String tableClassName) {
        Document document = Jsoup.parse(html);
        //获得文档下所有class属性为header的标签，返回的是一个标签的集合
        Map<String, String> map = new HashMap<>();
        Elements elements = document.getElementsByClass(tableClassName);
        List<String> list = new ArrayList<>();
        //定义一个标签对象
        Element element = null;
        //从这一个标签集合中一个一个的取出
        for (int i = 0; i < elements.size(); i++) {
            element = elements.get(i);
        }
        Elements trs = elements.select("table").select("tr");
        System.out.println("打印这个是什么-------------" + trs.text());
        //循环遍历tr标签
        Map<String, String> yigeMap = new HashMap<>();
        for (Element tr : trs) {
            //找到th标签
            Elements ths = tr.getElementsByTag("th");
            Elements tds = tr.getElementsByTag("td");

            for (int i = 0; i < ths.size(); i++) {
                if (tds.size() <= 0 && StringUtils.isNotBlank(ths.get(i).text())) {
                    //第一行只有th,但是有三个th
                    System.out.println("打印第一行" + ths.get(i).text());
                    map.put(ths.get(0).text(), ths.get(1).text());
                }
                for (int j = 0; j < tds.size(); j++) {
                    boolean empty = tds.get(j).text().isEmpty();
                    String tdvalue;
                    String name = ths.get(j).text();
                    if (empty) {
                        System.out.println("这个td的内容为空" + tds.get(j).text());
                        tdvalue = " ";
                    } else {
                        tdvalue = tds.get(j).text();
                        Elements zch = tds.get(j).select("span.comshow_16");
                        if (zch != null) {
                            String zhucehao = zch.text().replaceAll(" ", "");
                            if (StringUtils.isNotBlank(zhucehao)) {
                                //判断<td>21011<span>之间是否有字符串
                                String str = tds.get(j).toString();
                                Matcher matcher = Pattern.compile("<span").matcher(str);
                                int x = str.indexOf(">");
                                String tdStr = "";
                                if (matcher.find()) {
                                    tdStr = str.substring(x + 1, matcher.start());
                                }
                                tdvalue = tdStr + zhucehao;
                            }
                        }
                    }
                    map.put(name, tdvalue);
                }

            }
        }
        return map;
    }


    public static void main(String[] args) {
        String url = "https://www.qcc.com/firm/d31dda615bdb9dee26dcacdf382af5bd.html";
        String cookie = WebUtil.getCookie(url);
        String pageContent = WebUtil.getPageContent(url, cookie, null);
        Elements row_tags = Jsoup.parse(pageContent).getElementsByClass("newtags");
        System.out.println("打印这个内容："+row_tags);
        System.out.println("打印这个文本："+row_tags.text());

//        for (Element ele : row_tags) {
//            Elements span = ele.getElementsByTag("span");
//            System.out.println("dayin---"+span.text());
//        }
    }


    public static void main1(String[] args) throws Exception {
        String key = "安庆嘉润食品有限公司";
        String urlReq = "https://www.qcc.com/web/search?key=" + key;

        String linkClassName = "maininfo";
        Map<String, String> cookieUrlMap = WebUtil.getJumpUrl1(urlReq, linkClassName, "www.qcc.com");
        String cookie = cookieUrlMap.get("cookie");
        String url = cookieUrlMap.get("url");
        String html = WebUtil.getPageContent1(url, cookie, "www.qcc.com");
        Map<String, String> resultMap = WebUtil.resolveHtml(html, "panel b-a base_info");
        //注册地址进行特殊处理
        String value = resultMap.get("注册地址");
        String address = value.substring(0, value.indexOf(" "));
        resultMap.put("注册地址", address);
        System.out.println("打印map的值:" + new Gson().toJson(resultMap));
    }


    public static void main2(String[] args) throws UnsupportedEncodingException {
        String key = "沈阳含饴食品股份有限公司"; // 海口市琦权电子商务有限公司
        String urlForCookie = "https://www.11315.com/newsearch?regionMc=%E9%80%89%E6%8B%A9%E5%9C%B0%E5%8C%BA&regionDm=&searchType=1&searchTypeHead=1&name=" + URLEncoder.encode(key, "utf-8");
        System.out.println("打印请求地址：" + urlForCookie);
//        String linkClassName = "maininfo";//企查查
//        String linkClassName = "or_search_row_content";//水滴
        String linkClassName = "s_c_content fl";
        Map<String, String> result = getJumpUrl(urlForCookie, linkClassName);
        String cookie = result.get("cookie");
        String url = result.get("url");
        String html = getPageContent(url, cookie, null);
        Document document = Jsoup.parse(html);
        //获得文档下所有class属性为header的标签，返回的是一个标签的集合
        String tableClassName = "content";
        Map<String, String> map = new HashMap<>();
        Elements elements = document.getElementsByClass(tableClassName);
        List<String> list = new ArrayList<>();
        //定义一个标签对象
        Element element = null;
        //从这一个标签集合中一个一个的取出
        for (int i = 0; i < elements.size(); i++) {
            element = elements.get(i);
        }
        Elements trs = elements.select("table").select("tr");
        System.out.println("打印这个是什么-------------" + trs.text());
        //循环遍历tr标签
        Map<String, String> yigeMap = new HashMap<>();
        for (Element tr : trs) {
            //找到th标签
            Elements ths = tr.getElementsByTag("th");
            Elements tds = tr.getElementsByTag("td");

            for (int i = 0; i < ths.size(); i++) {
                if (tds.size() <= 0 && StringUtils.isNotBlank(ths.get(i).text())) {
                    //第一行只有th,但是有三个th
                    System.out.println("打印第一行" + ths.get(i).text());
                    map.put(ths.get(0).text(), ths.get(1).text());
                }
                for (int j = 0; j < tds.size(); j++) {
                    boolean empty = tds.get(j).text().isEmpty();
                    String tdvalue;
                    String name = ths.get(j).text();
                    if (empty) {
                        System.out.println("这个td的内容为空" + tds.get(j).text());
                        tdvalue = " ";
                    } else {
                        tdvalue = tds.get(j).text();
                        String str = tds.get(j).toString();
                        //特殊处理注册号
                        Elements zch = tds.get(j).select("span.comshow_16");
                        if (zch != null) {
                            String zhucehao = zch.text().replaceAll(" ", "");
                            if (StringUtils.isNotBlank(zhucehao)) {
                                //判断<td>21011<span>之间是否有字符串
                                System.out.println("打印td这一行标签内容：" + str);
                                Matcher matcher = Pattern.compile("<span").matcher(str);
                                int x = str.indexOf(">");
                                System.out.println("打印>的位置：" + x);
                                String tdStr = "";
                                if (matcher.find()) {
                                    System.out.println("匹配结果：" + matcher.start());
                                    tdStr = str.substring(x + 1, matcher.start());
                                }
                                tdvalue = tdStr + zhucehao;
                            }
                        }
                    }
                    map.put(name, tdvalue);

                }

            }
        }
        System.out.println("打印最终的map：" + new Gson().toJson(map));
        String add = map.get("注册地址");
        String addressValue = add.substring(0, add.indexOf(" "));
        map.put("注册地址", addressValue);
        String intro = map.get("单位简介");
        String introValue = intro.substring(0, intro.indexOf(" "));
        map.put("单位简介", introValue);
        System.out.println("打印map的值:" + new Gson().toJson(map));
    }
//        Elements ths = trs.select("th");
//        System.out.println("打印ths的大小："+ths.size());
//
//        StringBuilder build = new StringBuilder(trs.text());
//        build.insert(build.indexOf("法定代表人") + 5, " ");
//        if(!(ths.size() %2 == 0)){
//            System.out.println("zou1111111111111111111111111");
//            build.insert(build.indexOf("营业期限至") + 5, " ");
//        }
//        build.delete(build.indexOf("查看地图"), build.indexOf("查看地图") + 5);
//        build.delete(build.indexOf(">"), build.length());
//        String[] test = build.toString().split(" ");
//        for (int t = 0; t < test.length; t++) {
//            list.add(test[t]);
//        }
//        String zhucehao = "";
//        Elements tds = trs.select("td");
//        List<String> yylist = new ArrayList<>();
//        for (int d = 0; d < tds.size(); d++) {
//            String text = tds.get(d).text();
//            System.out.println("dayin这个td:" + text);
//
//            //TODO 正则表达式获取营业期限至之后的内容
//            String regex = "((?<=营业期限至).+(?=</tr))";
//
//            Pattern pattern = Pattern.compile(regex);
//            System.out.println("打印td的原来html内容："+tds.get(d));
//            System.out.println("打印td的原来内容："+tds.get(d).toString());
//            Matcher m  = pattern.matcher(trs.get(d).toString());
//            while (m.find()) {
//                int i = 1;
//                System.out.println("匹配到："+m.group(0));
//                yylist.add(m.group(i));
//                i++;
//            }
//            Elements zch = tds.get(d).select("span.comshow_16");
//            if (zch != null) {
//                String tihuan = zch.text().replaceAll(" ", "");
//                if (StringUtils.isNotBlank(tihuan)) {
//                    zhucehao = tihuan;
//                    System.out.println("打印这个是------" + zhucehao);
//                }
//            }
//        }
//
//        System.out.println("打印最终的keylist：" + new Gson().toJson(list));
//        Map<String, String> resMap = new HashMap<>();
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println("打印keylist的长度："+list.size());
//            if(i%2 ==0){
//                //放value
//                resMap.put(list.get(i), list.get(i + 1));
//            }
//
////            list.remove(i);
//        }
//        System.out.println("打印yylist："+new Gson().toJson(yylist));
//        System.out.println("打印resMap的值:" + new Gson().toJson(resMap));


}
